package com.dhq.myapp.base.divider

import android.content.Context
import android.graphics.Canvas
import android.graphics.Rect
import android.graphics.drawable.ColorDrawable
import android.graphics.drawable.Drawable
import android.view.View
import androidx.annotation.ColorRes
import androidx.annotation.DimenRes
import androidx.annotation.DrawableRes
import androidx.core.content.ContextCompat
import androidx.core.content.res.ResourcesCompat
import androidx.recyclerview.widget.RecyclerView

/**
 * DESC 分割线工具
 * Created by admin on 2017/11/17.
 */
abstract class DividerFactory : RecyclerView.ItemDecoration() {

    override fun onDraw(c: Canvas, parent: RecyclerView, state: RecyclerView.State) {
        drawDivider(c, parent, state)
    }

    override fun getItemOffsets(outRect: Rect, view: View, parent: RecyclerView, state: RecyclerView.State) {
        calculateItemOffsets(outRect, view, parent, state)
    }

    /**
     * 绘制分割线
     */
    abstract fun drawDivider(c: Canvas, parent: RecyclerView, state: RecyclerView.State)

    /**
     * 计算偏移量
     */
    abstract fun calculateItemOffsets(outRect: Rect, view: View, parent: RecyclerView, state: RecyclerView.State)

    /**
     * 添加分割线
     *
     * @param recyclerView target recyclerView
     */
    fun addTo(recyclerView: RecyclerView) {
        recyclerView.addItemDecoration(this)
    }

    companion object {
        fun builder(context: Context?): Builder {
            require(context != null) { "Context is null" }
            return Builder(context)
        }
    }

    class Builder(private val context: Context) {
        var drawable: Drawable? = null
        var columnSpace: Int = 0
        var rowSpace: Int = 0
        var hideLastDivider: Boolean = true

        fun buildLinearDivider(): LinearDivider {
            return LinearDivider(this)
        }

        fun buildGridDivider(): GridDivider {
            return GridDivider(this)
        }

        fun setSpaceColor(@ColorRes id: Int, @DimenRes strokeWidth: Int): Builder {
            this.drawable = ColorDrawable(ContextCompat.getColor(context, id))
            this.columnSpace = context.resources.getDimensionPixelSize(strokeWidth)
            this.rowSpace = context.resources.getDimensionPixelSize(strokeWidth)
            return this
        }

        fun setSpaceColor(@ColorRes id: Int): Builder {
            this.drawable = ColorDrawable(ContextCompat.getColor(context, id))
            return this
        }

        fun setColumnSpace(@DimenRes strokeWidth: Int): Builder {
            this.columnSpace = context.resources.getDimensionPixelSize(strokeWidth)
            return this
        }

        fun setRowSpace(@DimenRes strokeWidth: Int): Builder {
            this.rowSpace = context.resources.getDimensionPixelSize(strokeWidth)
            return this
        }

        fun setSpace(@DimenRes strokeWidth: Int): Builder {
            this.columnSpace = context.resources.getDimensionPixelSize(strokeWidth)
            this.rowSpace = context.resources.getDimensionPixelSize(strokeWidth)
            return this
        }

//        fun setDrawable(@DrawableRes id: Int): Builder {
//            this.drawable = ResourcesCompat.getDrawable(context.resources, id, context.theme)
//            return this
//        }

        fun setHideLastDivider(hideLastDivider: Boolean): Builder {
            this.hideLastDivider = hideLastDivider
            return this
        }

        // Getter methods
        fun getContext(): Context = context
//        fun getDrawable(): Drawable? = drawable
//        fun getColumnSpace(): Int = columnSpace
//        fun getRowSpace(): Int = rowSpace
        fun isHideLastDivider(): Boolean = hideLastDivider
    }
}